package com.primb.bench.project.home.model;

import com.primb.baselibs.util.JsonUtils;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.contract.AddEnsureInfoContract;
import com.primb.bench.project.home.entity.EnsureObjectEntity;
import com.primb.bench.project.home.entity.HttpApplyResultEntity;
import com.primb.bench.project.home.entity.HttpInfoEntity;
import com.primb.bench.project.home.entity.HttpLoanTreeEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;

import static com.primb.bench.project.home.model.AddInfoModelImpl.KEY_ENSURE_MAIN;

/**
 * Created by Administrator on 2017/05/17
 */

public class AddEnsureInfoModelImpl implements AddEnsureInfoContract.Model {
    public static final String KEY_ENSURE_MAIN_BZ = "L07";//主担保方式 保证
    public static final String KEY_ENSURE_MAIN_OT = "L10";//主担保方式

    @Override
    public Observable<List<KeyEntity>> getPickerList(int code, String type) {
        List<KeyEntity> dataList = new ArrayList<>();
        if (AppConstant.ADD_MAIN_TYPE == code) {
            Observable<HttpInfoEntity> otherObservable = ApiClient.initService(ApiService.class).getLoanInfoList(KEY_ENSURE_MAIN_OT);
            return otherObservable.flatMap(new Func1<HttpInfoEntity, Observable<List<KeyEntity>>>() {

                @Override
                public Observable<List<KeyEntity>> call(HttpInfoEntity httpInfoEntity) {
                    try {
                        convertData(dataList, httpInfoEntity);
                    } catch (Exception e) {
                        Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                    }
                    return Observable.just(dataList);
                }
            }).compose(RxSchedulerUtils.normalSchedulersTransformer());
        } else {
            return ApiClient.initService(ApiService.class).getLoanConcretenessList(type).flatMap(new Func1<HttpLoanTreeEntity, Observable<List<KeyEntity>>>() {
                @Override
                public Observable<List<KeyEntity>> call(HttpLoanTreeEntity httpCurreyEntity) {
                    try {
                        convertList(dataList, httpCurreyEntity.getData());
                    } catch (Exception e) {
                        Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                    }
                    return Observable.just(dataList);
                }
            }).compose(RxSchedulerUtils.normalSchedulersTransformer());
        }
    }

    @Override
    public Observable<HttpStrEntity> saveEnsureObject(EnsureObjectEntity entity, String priceId, boolean isNew) {
        HashMap<String, String> map = new HashMap<>();
        if (isNew)
            map.put("cmd", "ADD");
        else
            map.put("cmd", "EDIT");
        map.put("collateral", entity.getPawnCode());
        map.put("collaAmt", entity.getPawnAmt());
        map.put("guatantType", entity.getGuatantCode());
        map.put("priceId", priceId);
        if (entity.getGuatantId() != null && !entity.getGuatantId().equals(""))
            map.put("guatantId", entity.getGuatantId());
        else
            map.put("guatantId", "");
        return ApiClient.initService(ApiService.class).saveEnsureObject(map)
                .compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static void convertList(List<KeyEntity> dataList, List<HttpLoanTreeEntity.DataBean> entity) {
        for (int i = 0; i < entity.size(); i++) {
            if (entity.get(i).getLeafFlag() != null && entity.get(i).getLeafFlag().equals("1")) {
                dataList.add(new KeyEntity(entity.get(i).getText(), entity.get(i).getId()));
            } else {
                convertList(dataList, entity.get(i).getChildren());
            }
        }
    }

    public static void convertData(List<KeyEntity> dataList, HttpInfoEntity httpCurreyEntity) {
        for (int i = 0; i < httpCurreyEntity.getData().size(); i++) {
            KeyEntity keyEntity = new KeyEntity();
            keyEntity.setKey(httpCurreyEntity.getData().get(i).getDesc());
            keyEntity.setValue(httpCurreyEntity.getData().get(i).getCode());
            dataList.add(keyEntity);
        }
    }
}